﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLUNZ.Business;

namespace NLUNZ.UserControls
{
    public partial class DeliveryItemEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
            resultItem.Visible = false;
            emptyItem.Visible = false;
            DataBind();
            }
        }
        private void DataBind()
        {
            Custom emptyCustom = new Custom() {ID = 0, Name = "选择海关"};

            List<Custom> customs = new List<Custom>();
            customs.Add(emptyCustom);
            customs.AddRange(Repository.GetAllCustom());
            DropDownListCustoms.DataSource = customs;
            DropDownListCustoms.DataTextField = "Name";
            DropDownListCustoms.DataValueField = "ID";
            DropDownListCustoms.DataBind();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string searchKey = txtDeliverySearchBox.Text.Trim();

            if (searchKey.Length == 0){
                emptyItem.InnerHtml = Utility.InfoHtml("请输入运单号码查询。");
                emptyItem.Visible = true;
                return;
            }
                //Key should exactly match
            Delivery delivery = Repository.GetDeliveryByDeliveryNumber(searchKey);
            if(delivery != null)
            {   
                DeliveryDataBind(delivery);
                resultItem.Visible = true;
                updateInfo.Visible = false;
                emptyItem.Visible = false;
            }
            else
            {
                resultItem.Visible = false;
                emptyItem.Visible = true;
                emptyItem.InnerHtml = Utility.InfoHtml("没有运单结果。请重新确认运单号重试！");
            }
        }

        private void DeliveryDataBind(Delivery delivery)
        {
            txtDeliveryNumber.Text = delivery.DeliveryNum;
            txtRecipient.Text = delivery.Recipient;
            txtDeliveryCity.Text = delivery.DeliveryCity;
            receiveDate.Value = Utility.GetKendoDateTime(delivery.ReceivedDateTime);
            inventoryDate.Value = Utility.GetKendoDateTime(delivery.InventoryDateTime);
            arriveAirportDate.Value = Utility.GetKendoDateTime(delivery.AirportDateTime);
            deliveryDate.Value = Utility.GetKendoDateTime(delivery.DeliveryDateTime);
            customClearDate.Value = Utility.GetKendoDateTime(delivery.CustomDate);
            DropDownListCustoms.SelectedValue = GetCustomID(delivery.Custom) ;
            if (delivery.IsCheckedOut == true)
                CheckBoxCheckedOut.Checked = true;

        }

        private string GetCustomID(string custom)
        {
            int id = Repository.GetCustomID(custom);
            return id.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool hasError = false;
            updateInfo.Visible = false;
            string customKey = DropDownListCustoms.SelectedValue;
            int customId = int.Parse(customKey);
            string customName = customId == 0 ? "Empty" : Repository.GetCustomByID(customId);

            if(string.IsNullOrEmpty(customName))
            {
                hasError = true;
            }
            else
            {
                
                Delivery delivery = new Delivery() {DeliveryNum = txtDeliveryNumber.Text};
                delivery.IsCheckedOut = CheckBoxCheckedOut.Checked;
                delivery.ReceivedDateTime = Utility.SetKendoDateTime(receiveDate.Value);
                delivery.InventoryDateTime = Utility.SetKendoDateTime(inventoryDate.Value);
                delivery.AirportDateTime = Utility.SetKendoDateTime(arriveAirportDate.Value);
                delivery.DeliveryDateTime = Utility.SetKendoDateTime(deliveryDate.Value);
                delivery.DeliveryCity = txtDeliveryCity.Text;
                delivery.CustomDate = Utility.SetKendoDateTime(customClearDate.Value);
                delivery.Recipient = txtRecipient.Text;
                if(!customName.Equals("Empty"))
                {
                    delivery.Custom = customName;
                }
                    
                if (!Repository.UpdateDeliveryRecord(delivery))
                    hasError = true;
            }
            if (hasError)
                updateInfo.InnerHtml = Utility.ErrorHtml("运单更新出错，请确认后再试！");
            else
            {
                updateInfo.InnerHtml = Utility.SuccessHtml(string.Format("运单 {0} 已经更新成功!", txtDeliveryNumber.Text));
            }
            updateInfo.Visible = true;
        }
    }
}